<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class shop_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $cquery = $this->module('category')->get_top_list();
        if ($cquery) {
            foreach ($cquery as $k => $v) {
                $cdata[$v['cid']] = $v['name'];
            }
        }
        $type_arr = $this->module('shop')->type();
        $wsql = '';
        $type = isset($_GET['type']) ? $_GET['type'] : '';
        if ($type) {
            $wsql .= ' type = ' . $type . ' and ';
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $sql = "select * from " . table('shop') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $query = $this->db->get_all($sql);
        if ($query) {
            foreach ($query as $k => $v) {
                if (isset($cdata[$v['cid']])) {
                    $v['cname'] = $cdata[$v['cid']];
                }
                $v['addtime'] = _date($v['addtime']);
                $v['type_cn'] = $type_arr[$v['type']];
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('shop') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = ${$this}->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function px_status()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : exit('error');
        $type = $_POST['type'];
        $this->db->query("update " . table('shop') . " set {$type} = ({$type} + 1)%2 where id = {$id} limit 1");
        $row = $this->db->get_one("select * from " . table('shop') . " where id = {$id}");
        echo $row[$type];
    }
    public function edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $this->assign('row', $this->module('shop')->get_one($id));
        }
        $this->assign('shop_type_row', $this->module('category')->get_top_list());
        $this->assign('shop_type', $this->module('shop')->type());
        $this->display();
    }
    public function save()
    {
        $setsql['type'] = P('type', '店铺类型不能为空');
        $setsql['cid'] = P('cid', '店铺分类不能为空');
        $setsql['name'] = P('name', '店铺名称不能为空');
        $setsql['mobile'] = P('mobile');
        $setsql['contact'] = P('contact');
        $setsql['act_status'] = P('act_status', '', '0');
        $setsql['ratio'] = P('ratio', '', '0');
        $setsql['status'] = P('status', '', '0');
        $spid = P('id', false);
        if ($spid) {
            $query = $this->db->update(table('shop'), $setsql, 'id = ' . $spid);
        } else {
            $query = $this->db->add(table('shop'), $setsql);
            $setsql['addtime'] = time();
        }
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    function del()
    {
        $id = isset($_GET['id']) ? intval($_GET['id']) : showmsg('提交错误');
        $row = $this->db->get_one("select * from " . table("shop") . " where id = " . $id);
        $setarr['status'] = '2';
        $setarr['auth_reason'] = '创建店铺已删';
        $this->db->update(table('shop_auth'), $setarr, 'uid = ' . $row['uid']);
        $this->db->delete(table('shop'), 'id = ' . $id);
        $this->db->delete(table('shop_address'), 'spid = ' . $id);
        $this->db->delete(table('shop_bill'), 'shop_id = ' . $id);
        $this->db->delete(table('shop_design'), 'shop_id = ' . $id);
        $this->db->delete(table('shop_express'), 'shop_id = ' . $id);
        $this->db->delete(table('shop_score'), 'shop_id = ' . $id);
        $this->db->delete(table('promote_reduce'), 'shop_id = ' . $id);
        $this->db->delete(table('promote_seckill'), 'shop_id = ' . $id);
        $this->db->delete(table('promote_seckill_sku'), 'shop_id = ' . $id);
        $this->db->delete(table('goods_group'), 'shop_id = ' . $id);
        $this->db->delete(table('collect_shop'), 'spid = ' . $id);
        $this->db->delete(table('cart'), 'shop_id = ' . $id);
        $all = $this->db->get_all("select * from " . table("goods") . " where shop_id = " . $id);
        if ($all) {
            foreach ($all as $k => $v) {
                $this->db->delete(table('goods_attr_index'), 'gid = ' . $v['id']);
                $this->db->delete(table('goods_img'), 'gid = ' . $v['id']);
                $this->db->delete(table('goods_sku_img'), 'gid = ' . $v['id']);
                $this->db->delete(table('goods_sku_index'), 'gid = ' . $v['id']);
                $this->db->delete(table('collect_goods'), 'gid = ' . $v['id']);
            }
        }
        $this->db->query("update " . table('album') . " set type = 0 where shop_id = " . $id);
        showmsg('删除成功');
    }
    public function enter()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : exit('ID参数错误！');
        showmsg('进入店铺', 'index.php?m=shop&id=' . $id, '0');
    }
    public function enter_shop()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : exit('ID参数错误！');
        $url = isset($_GET['url']) ? $_GET['url'] : '';
        $row = $this->db->get_one("select * from " . table('shop') . " where id = " . $id);
        if ($row['id']) {
            $this->module('user')->create_shop_session($row);
            if (empty($url)) {
                echo '<script>location.href="member.php?m=shop";</script>';
            } else {
                echo '<script>location.href="' . $url . '";</script>';
            }
        } else {
            showmsg('店铺不纯在');
        }
    }
    public function score()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('店铺不能为空');
        if ($id) {
            $row = $this->db->get_one("select id,name from " . table('shop') . " where id = " . $id);
            $this->assign('row', $row);
            $srow = $this->db->get_one("select * from " . table("shop_score") . " where id = " . $id);
            $this->assign('srow', $srow);
        }
        $this->display();
    }
    public function score_save()
    {
        if ($_POST) {
            $setarr['goods'] = P('goods', '', '5.00');
            $setarr['goods_score'] = P('goods_score', '', '0');
            $setarr['goods_num'] = P('goods_num', '', '0');
            $setarr['service'] = P('service', '', '5.00');
            $setarr['service_score'] = P('service_score', '', '0');
            $setarr['service_num'] = P('service_num', '', '0');
            $setarr['logistics'] = P('logistics', '', '5.00');
            $setarr['logistics_score'] = P('logistics_score', '', '0');
            $setarr['logistics_num'] = P('logistics_num', '', '0');
            $id = P('id', false);
            if ($id) {
                $query = $this->db->update(table('shop_score'), $setarr, 'id = ' . $id);
                if ($query) {
                    right_json();
                } else {
                    error_json('保存失败');
                }
            } else {
                error_json('操作错误');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function auth()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $shop_type = $this->module('shop')->type();
        $query = $this->db->get_all("select * from " . table('shop_auth') . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($query) {
            foreach ($query as $k => $v) {
                $user_arr[] = $v['uid'];
            }
            if (isset($user_arr)) {
                $user_in = implode(',', $user_arr);
                $user_all = $this->db->get_all("select * from " . table("shop") . " where uid in(" . $user_in . ")");
                if ($user_all) {
                    foreach ($user_all as $k => $v) {
                        $sp_user[$v['uid']] = $v['name'];
                    }
                }
            }
            foreach ($query as $k => $v) {
                $v['type_cn'] = $shop_type[$v['shop_type']];
                $v['auth_time'] = _date($v['auth_time']);
                $v['apply_time'] = _date($v['apply_time']);
                $v['open_status'] = isset($sp_user[$v['uid']]) ? '1' : '0';
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('shop_auth'));
            if ($page_size < $total['total']) {
                $data['page'] = ${$this}->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function auth_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table("shop_auth") . " where id = " . $id . " limit 1");
            if ($row && $row['id']) {
                $row['license_start_time'] = isset($row['license_start_time']) ? date('Y-m-d', $row['license_start_time']) : '';
                $row['license_end_time'] = isset($row['license_end_time']) ? date('Y-m-d', $row['license_end_time']) : '';
                $row['organization_start_time'] = isset($row['organization_start_time']) ? date('Y-m-d', $row['organization_start_time']) : '';
                $row['organization_end_time'] = isset($row['organization_end_time']) ? date('Y-m-d', $row['organization_end_time']) : '';
                $this->assign('row', $row);
            }
        }
        $this->display('shop/auth_edit');
    }
    function auth_submit()
    {
        if ($_POST) {
            $id = P('id', 'POST ERROR');
            $row = $this->db->get_one("select * from " . table("shop_auth") . " where id = " . $id . " limit 1");
            if ($row && $row['id']) {
                $status = isset($_POST['status']) ? intval($_POST['status']) : error_json('认证状态不能为空');
                if ($status == '1') {
                    $shop_row = $this->db->get_one("select * from " . table("shop") . " where uid = " . $row['uid']);
                    if ($shop_row) {
                        $this->db->autocommit();
                        if ($shop_row['id']) {
                            $spsetsql['type'] = $row['shop_type'];
                            $spsetsql['status'] = '1';
                            $query = $this->db->update(table('shop'), $spsetsql, 'uid = ' . $row['uid']);
                        } else {
                            $spsql['uid'] = $row['uid'];
                            $spsql['type'] = $row['shop_type'];
                            $spsql['status'] = '1';
                            $spsql['addtime'] = time();
                            $query = $this->db->add(table('shop'), $spsql);
                        }
                        $setsql['auth_reason'] = '--';
                        $setsql['auth_time'] = time();
                        $setsql['status'] = '1';
                        $query2 = $this->db->update(table('shop_auth'), $setsql, 'id = ' . $id);
                        if ($query && $query2) {
                            $this->db->commit();
                            right_json();
                        } else {
                            $this->db->rollback();
                            error_json('开通失败');
                        }
                    } else {
                        error_json('错误:无法获取到店铺');
                    }
                } elseif ($status == '2') {
                    $this->db->autocommit();
                    $spsetsql['type'] = $row['shop_type'];
                    $spsetsql['status'] = '2';
                    $query = $this->db->update(table('shop'), $spsetsql, 'uid = ' . $row['uid']);
                    $setsql['auth_reason'] = P('auth_reason');
                    $setsql['status'] = '2';
                    $query2 = $this->db->update(table('shop_auth'), $setsql, 'id = ' . $id);
                    if ($query && $query2) {
                        $this->db->commit();
                        right_json();
                    } else {
                        $this->db->rollback();
                        error_json('操作失败');
                    }
                }
            } else {
                error_json('无店铺申请信息');
            }
        } else {
            error_json('提交失败');
        }
    }
    function auth_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('GET ID ERROR');
        $row = $this->db->get_one("select * from " . table('shop_auth') . " where id = " . $id);
        if ($row['status'] == '1') {
            showmsg('已经通过，无法删除！');
        } else {
            $query = $this->db->query("update " . table('album') . " set type = 0 where uid = " . $row['uid'] . " and type = 6");
            $query1 = $this->db->delete(table('shop_auth'), 'id = ' . $id);
            if ($query && $query1) {
                showmsg('删除成功');
            } else {
                showmsg('删除失败');
            }
        }
    }
}